#ifndef UARTCHANNEL_H
#define UARTCHANNEL_H

#include <string>
#include <thread>
#include <atomic>
#include <mutex>
#include <functional>
#include "SerialPort.h" // 假设你有一个 SerialPort 类用于底层串口操作

class UartChannel {
public:
    using ReceiveCallback = std::function<void(const std::string&)>;
    using StatusCallback = std::function<void(bool)>; // 状态回调函数

    UartChannel(const std::string& portName, int baudRate);
    ~UartChannel();

    bool open();
    void close();
    bool send(const std::string& data);
    void setReceiveCallback(ReceiveCallback callback);
    void setStatusCallback(StatusCallback callback);
    bool isConnected() const;

private:
    void receiveData();

    std::string portName;
    int baudRate;
    SerialPort serialPort;
    std::atomic<bool> isOpen;
    std::atomic<bool> isRunning;
    std::thread receiveThread;
    ReceiveCallback receiveCallback;
    StatusCallback statusCallback; // 状态回调
    std::mutex mutex;
};

#endif // UARTCHANNEL_H